﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BasicFrame.WebControls;
using flsfragusseModel;





class Contractinformation
{
   public string afNamn;
   public string SalesNamn;
   public string kundnamn;
   public string contractnr;
   public string orgnrorpersnr;
   public string telefonnummer;
   public string adress;
   public string postort;
   public string legitimation;
   public DateTime leveransdag;
   public string bilmarke;
   public string bilmodell;
   public string chassinr;
   public string regnr;
   public DateTime regdag;
   public DateTime contractstart;
   public string meterreadingcontractstart;
   public string meterreadingcontractend;
   public DateTime contractstop;
   public string calculatedYearlydriving;
   public string contracttotaldriven;
   public string monthpayment;
   public string invoicecost;
   public DateTime duedate;
   public string contractlength;
   public string paymentfrequence;
   public string signort;
   public DateTime signdate;
}
public enum modeUser
{
    SALESMEN = 1,
    ADMINISTRATOR
}
public partial class partner_controls_viewContracts : System.Web.UI.UserControl
{
    bool filldata = false;
  
   
   //mode salesmen=1,admin=2,
    public int mode =-1;

 //   public int partnerid = 3;
 //   public int productid = 17;
    //finland
   // public int countryID = 4;
    public int gvehicleID = 0;
    public int g_productid = -1;
    public int g_partnerid = -1;
    public int g_countryid = -1;
    public string countryCurrency = string.Empty;
 //   string pricelistCategory = "n1";
    string invoiceFee = string.Empty;
    flsfragusseEntities1 cEntity = new flsfragusseEntities1();
    public GridView grdview = null;
    public void Start()
    {
        if (!IsPostBack)
        {
        //    Fillcombos();
            EnableDisableButtons();

          
        }


    }

    public void SetDatagridview(GridView grdview)
    {
        this.grdview = grdview;
    }

    public void FillGrid()
    {
     
        grdview.DataSource = from p in cEntity.tblVehicle_change
                             where p.ExportOperation == "Cancellation" || p.ExportOperation == "ContractModification"
                             select p;
        grdview.DataBind();

       
    }
    private void EnableDisableButtons()
    {
        //admin
        if (mode == Convert.ToInt32(modeUser.ADMINISTRATOR))
        {
            btnApprove.Visible = true;
            btnDissaprove.Visible = true;

         //   FillVehicle(4662);

        }
       
    }

    private void GetCouuntryCurrency(int countryID)
    {

        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
         var result = from p in cEntity.tblCountry
                      where p.countryID == countryID
                      select new { Currency = p.countryCurrency };
         foreach (var rec in result)
         {
             countryCurrency=Convert.ToString(rec.Currency);
         }
    }

    private void GetinvoiceFeeForPartner(int partnerid)
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        var result = from p in cEntity.tblPartner
                     where p.partnerID==partnerid
                     select new { fee=p.invoiceFee };
        foreach (var rec in result)
        {
            
            invoiceFee =Convert.ToString(rec.fee);
        }
    }
   
    private void SaveVechicle(int changeID, int vehicleID,int productID, int pricelistduration, int priceListDistance, int fueltype, int cylinderVolume, int serviceintervall, bool isOkayChanges,int modelID)
     
    {
            flsfragusseEntities1 cEntity = new flsfragusseEntities1();

            tblVehicle_change vc = new tblVehicle_change();   
            //Hämta ut den gamla posten
            //behöver vilken typ av changeoperation vi ska utföra, terminate/modifi contract
            List<tblVehicle_change> lstvehicle_change = (from p in cEntity.tblVehicle_change
                                           where p.ID == changeID
                                           select p).ToList();
            if (lstvehicle_change.Count() > 0)
            {
                vc = lstvehicle_change[0];
            }

            //hämta originallistan
            tblVehicle v = new tblVehicle();    
  
        //sverige och norge
            int pricelistID = 0;
            if (productID == 25 || productID == 19)
            {

                var result = from p in cEntity.tblPriceList
                             join cm in cEntity.CommonModel on p.modelID equals cm.ID
                             join mc in cEntity.ModelCrossCommonModel on cm.ID equals mc.CommonModelID
                             orderby p.priceListCategory
                             where p.productID == productID && p.priceListDuration == pricelistduration && p.priceListDistance == priceListDistance && p.isDieselcar == fueltype && p.ServiceIntervall ==                               serviceintervall && p.cylinderVolumeFrom == cylinderVolume && mc.ModelID == modelID
                             select
                             new { price = p.price, pricelistID = p.priceListID };
                //).Distinct();
               
                foreach (var rec in result)
                {
                    //spara undran prislistid
                    pricelistID = rec.pricelistID;
                }
            }
            else
            {

                //hämta ut rätt pris baserat på valen
                var result = from p in cEntity.tblPriceList
                             orderby p.priceListCategory
                             where p.productID == productID && p.priceListDuration == pricelistduration && p.priceListDistance == priceListDistance && p.isDieselcar == fueltype && p.ServiceIntervall ==                               serviceintervall && p.cylinderVolumeFrom <= cylinderVolume && p.cylinderVolumeTo >= cylinderVolume
                             select
                             new { price = p.price, pricelistID = p.priceListID };
                //).Distinct();
                foreach (var rec in result)
                {
                    //spara undran prislistid
                    pricelistID = rec.pricelistID;
                }
            }


            //*hämta ut originalposten
            //hämta förändringsposten
            List<tblVehicle> lstvehicle = (from p in cEntity.tblVehicle
                                                        where p.vehicleID == vehicleID
                                                        select p).ToList();
            if (lstvehicle.Count() > 0)
            {
                v = lstvehicle[0];
            }

             v.priceListID = pricelistID;
              v.brandID = Convert.ToInt32(drpCarBrand.SelectedValue);
              v.modelID = Convert.ToInt32(drpModell.SelectedValue);
              v.isDieselcar = Convert.ToInt32(drpFueltype.SelectedValue);
              v.cylinderVolume = Convert.ToInt32(drpCylinderVolume.SelectedValue);
              v.regnr = txtRegnr.Text;
              v.vinNumber = txtVinnumber.Text;


              if (bdpRegdag.SelectedDate.ToString() != "0001-01-01 00:00:00")
                   v.firstRegistrationDate = bdpRegdag.SelectedDate;

               if (bdpStartDate.SelectedDate.ToString() != "0001-01-01 00:00:00")  
               v.leaseStartDate = bdpStartDate.SelectedDate;

               //produkten
               v.productID = Convert.ToInt32(drpProduct.SelectedValue);
               v.ExportToSantander = 1;
               v.FromPartnerWeb = 1;

               if (!isOkayChanges)
                   v.ExportToSantander = 0;

               v.ExportOperation = vc.ExportOperation;// "ContractModification";// rec.ExportOperation;
               v.date = DateTime.Now;



                //är ändringarna okay då kan vi spara dem 
                 if (isOkayChanges)
                 {

                     //uppdatera den nya posten
               //    cEntity.tblVehicle.Attach(v);
        
                   cEntity.ObjectStateManager.ChangeObjectState(v, System.Data.EntityState.Modified);
                   cEntity.SaveChanges();
                 }
                
      
               // vi tart alltid bort den gamla posten 
             //   cEntity.tblVehicle_change.Attach(vc);
                 cEntity.ObjectStateManager.ChangeObjectState(vc, System.Data.EntityState.Deleted);
                 cEntity.SaveChanges();

                //uppdatera gridden
                 FillGrid();
                 ClearFields();
    }

    private void ClearFields()
    {
        ClearBrand();
        ClearBrandModel();
        ClearServiceIntervall();
        ClearProduct();
        ClearDuration();
        ClearDrivingDistance();
        ClearFuelType();
        ClearCylindervolume();

        txtRegnr.Text = "";
        txtVinnumber.Text = "";
        bdpRegdag.Clear();
        bdpStartDate.Clear();
        grdview.SelectedIndex=-1;
        lblBetalningsmetod.Text = "";
        ClearChanges();
     }
    private void ClearFuelType()
    {
        List<Fueltype> lst = new List<Fueltype>();

        drpFueltype.DataSource = lst;
        drpFueltype.DataTextField = "Name";
        drpFueltype.DataValueField = "Value";

        drpFueltype.DataBind();
    }
    private void ClearDuration()
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpDuration.DataSource = (from p in cEntity.tblPriceList


                                  orderby p.priceListDuration
                                  where p.productID == -999
                                  select
                                  new { Name = p.priceListDuration, ID = p.priceListDuration }).Distinct();


        drpDuration.DataTextField = "Name";
        drpDuration.DataValueField = "ID";
        drpDuration.DataBind();
    }

    private void ClearServiceIntervall()
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpServiceIntervall.DataSource = (from p in cEntity.tblPriceList
                                          orderby p.ServiceIntervall
                                          where p.productID == -999
                                          select
                                          new { Name = p.ServiceIntervall }).Distinct();


        drpServiceIntervall.DataTextField = "Name";
        drpServiceIntervall.DataValueField = "Name";
        drpServiceIntervall.DataBind();
    }
    private void ClearBrandModel()
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpModell.DataSource = from p in cEntity.tblModel
                               orderby p.model
                               where p.brandID == -999
                               select
                               new { Name = p.model, ID = p.modelID };


        drpModell.DataTextField = "Name";
        drpModell.DataValueField = "ID";
        drpModell.DataBind();
    }
    private void ClearCylindervolume()
    {

        List<Cylindervolume> lst = new List<Cylindervolume>();
      
        drpCylinderVolume.DataSource = lst;
        drpCylinderVolume.DataTextField = "Name";

        drpCylinderVolume.DataBind();
       
    }
    private void ClearProduct()
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpProduct.DataSource = (from p in cEntity.tblProduct
                                 orderby p.product
                                 where p.productID == -999
                                 select
                                 new { Name = p.product, ID = p.productID });


        drpProduct.DataTextField = "Name";
        drpProduct.DataValueField = "ID";
        drpProduct.DataBind();
    }
    private void ClearDrivingDistance()
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();

        drpDrivingDistance.DataSource = (from p in cEntity.tblPriceList


                                         orderby p.priceListDuration
                                         where p.productID == -999 && p.priceListDuration == -999
                                         select
                                         new { Name = p.priceListDistance, ID = p.priceListDistance }).Distinct();

        drpDrivingDistance.DataTextField = "Name";
        drpDrivingDistance.DataValueField = "ID";
        drpDrivingDistance.DataBind();
    }
    private void Fillcombos(int productid,int partnerid,int countryid)
    {
        g_productid = productid;
        g_partnerid= partnerid;
        g_countryid = countryid;
        ClearChanges();
        GetCouuntryCurrency(countryid);
        GetinvoiceFeeForPartner(partnerid);
        filldata = true;
        FillBrand();
       

        FillbrandModel(Convert.ToInt32(drpCarBrand.SelectedValue));
        FillServiceIntervall(productid);
        FillCylinderVolume();
        FillProduct(productid);
        FillDuration(productid);
        FillDrivingDistance(productid, Convert.ToInt32(drpDuration.SelectedValue));

         FillFuelType();
         filldata = false;
         FillPaymentMethod(productid, Convert.ToInt32(drpDuration.SelectedValue), Convert.ToInt32(drpDrivingDistance.SelectedValue), Convert.ToInt32(drpFueltype.SelectedValue), Convert.ToInt32(drpCylinderVolume.SelectedValue), Convert.ToInt32(drpServiceIntervall.SelectedValue),partnerid,countryid,Convert.ToInt32(drpModell.SelectedValue));


       
        // FillDealerEmployees(Convert.ToInt32(drpDealers.SelectedValue));


    
    }

    private void UpdatePrice()
    {
        if (!filldata)
            FillPaymentMethod(g_productid, Convert.ToInt32(drpDuration.SelectedValue), Convert.ToInt32(drpDrivingDistance.SelectedValue), Convert.ToInt32(drpFueltype.SelectedValue), Convert.ToInt32(drpCylinderVolume.SelectedValue), Convert.ToInt32(drpServiceIntervall.SelectedValue),g_partnerid,g_countryid,Convert.ToInt32(drpModell.SelectedValue));

    }

    private void FillBrand()
    {
     //   Select brandID, brand From tblBrand where brandid=76 Order By brand
        //only nissan cars

        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpCarBrand.DataSource = from p in cEntity.tblBrand
                                 orderby p.brand 
                                 where p.brandID==76
                                 select
                                 new { Name = p.brand, ID = p.brandID };


        drpCarBrand.DataTextField = "Name";
        drpCarBrand.DataValueField = "ID";
        drpCarBrand.DataBind();
    }
    private void ClearBrand()
    {

        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpCarBrand.DataSource = from p in cEntity.tblBrand
                                 orderby p.brand
                                 where p.brandID == 111176
                                 select
                                 new { Name = p.brand, ID = p.brandID };

        drpCarBrand.DataBind();
    }


    public class Fueltype
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }

    private void FillFuelType()
    {
        List<Fueltype> lst = new List<Fueltype>();
       
        lst.Add(new Fueltype { Name ="Diesel", Value=1,  });
        lst.Add(new Fueltype { Name = "Bensin", Value =2, });
       
        drpFueltype.DataSource = lst;
        drpFueltype.DataTextField = "Name";
        drpFueltype.DataValueField = "Value";

        drpFueltype.DataBind();

    }
    private void FillbrandModel(int brandid)
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpModell.DataSource = from p in cEntity.tblModel
                                 orderby p.model
                                 where p.brandID == brandid
                                 select
                                 new { Name = p.model, ID = p.modelID };


        drpModell.DataTextField = "Name";
        drpModell.DataValueField = "ID";
        drpModell.DataBind();
    }

    //SELECT     distinct ServiceIntervall FROM   tblPriceList WHERE     (productID = 17)"

    private void FillServiceIntervall(int productID)
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpServiceIntervall.DataSource = (from p in cEntity.tblPriceList
                               orderby p.ServiceIntervall
                                          where p.productID == productID
                               select 
                               new { Name = p.ServiceIntervall }).Distinct();


        drpServiceIntervall.DataTextField = "Name";
        drpServiceIntervall.DataValueField = "Name";
        drpServiceIntervall.DataBind();
    }

    private void FillProduct(int productID)
    {
     //   Select productID, product From tblProduct Where partnerID

        
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpProduct.DataSource = (from p in cEntity.tblProduct
                                          orderby p.product
                                          where p.productID == productID
                                          select
                                          new { Name = p.product,ID=p.productID  });


        drpProduct.DataTextField = "Name";
        drpProduct.DataValueField = "ID";
        drpProduct.DataBind();
    }

    //     Select priceListDuration From tblPriceList Where productID = " & productID & " GROUP BY priceListDuration ORDER BY priceListDuration

    private void FillDuration(int productID)
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpDuration.DataSource = (from p in cEntity.tblPriceList
                            
                                
                                 orderby p.priceListDuration
                                 where p.productID == productID
                                 select
                                 new { Name = p.priceListDuration, ID = p.priceListDuration }).Distinct();


        drpDuration.DataTextField = "Name";
        drpDuration.DataValueField = "ID";
        drpDuration.DataBind();
    }

    private void FillDrivingDistance(int productID, int pricelistduration)
    {

        //  RecSet = Conn.Execute("Select priceListDistance From tblPriceList Where productID = " & productID & " And priceListDuration = " & priceListDuration & " GROUP BY priceListDistance ORDER BY priceListDistance")
      
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();

        drpDrivingDistance.DataSource = (from p in cEntity.tblPriceList
                            
                                
                                 orderby p.priceListDuration
                                 where p.productID == productID && p.priceListDuration==pricelistduration
                                 select
                                 new { Name = p.priceListDistance, ID = p.priceListDistance }).Distinct();

        drpDrivingDistance.DataTextField = "Name";
        drpDrivingDistance.DataValueField = "ID";
        drpDrivingDistance.DataBind();
    }
        

         

    private void FillPaymentMethod(int productID, int pricelistduration,int priceListDistance,int fueltype,int cylinderVolume,int serviceintervall,int partnerid, int countryid,int modelID)
    {
        //uppdatera priser om inte combosar laddar



        GetCouuntryCurrency(countryid);
            GetinvoiceFeeForPartner(partnerid);
            //     sql = "Select priceListID, priceListCategory, price From tblPriceList Where productID = " & productID & " And priceListDuration = " & priceListDuration & " And priceListDistance = " & priceListDistance & " And isDieselcar = " & fueltype & " And cylinderVolumeFrom <= " & cylinderVolume & " And cylinderVolumeTo >= " & cylinderVolume & " And ServiceIntervall = " & serviceintervall & " ORDER BY priceListCategory"



            //  RecSet = Conn.Execute("Select priceListDistance From tblPriceList Where productID = " & productID & " And priceListDuration = " & priceListDuration & " GROUP BY priceListDistance ORDER BY priceListDistance")

            flsfragusseEntities1 cEntity = new flsfragusseEntities1();
     
        //om norge eller sverige.
            if (productID == 25 || productID == 19)
            {

                var result = from p in cEntity.tblPriceList
                             join cm in cEntity.CommonModel on p.modelID equals cm.ID
                             join mc in cEntity.ModelCrossCommonModel on cm.ID equals mc.CommonModelID
                             orderby p.priceListCategory
                             where p.productID == productID && p.priceListDuration == pricelistduration && p.priceListDistance == priceListDistance && p.isDieselcar == fueltype && p.ServiceIntervall == serviceintervall && p.cylinderVolumeFrom == cylinderVolume && mc.ModelID == modelID
                             select
                            new { price = p.price, ID = p.priceListDistance };
                 foreach (var rec in result)
            {
                System.Diagnostics.Debug.WriteLine("productid: {0}, pricelistduration: {1}, pricelistdistance {2}, isdieselcar {3}, cylindervolume {4}, serviceintervall {5}",
                    productID.ToString(), pricelistduration.ToString(), priceListDistance.ToString(), fueltype.ToString(), cylinderVolume.ToString(), serviceintervall.ToString());
                
                lblBetalningsmetod.Text = string.Format("Månad- {0} {1} + {2} {3} / månad", pricelistduration,rec.price, invoiceFee, countryCurrency);
            }
            }
            else
            {
                var result = from p in cEntity.tblPriceList
                             orderby p.priceListCategory
                             where p.productID == productID && p.priceListDuration == pricelistduration && p.priceListDistance == priceListDistance && p.isDieselcar == fueltype && p.ServiceIntervall == serviceintervall && p.cylinderVolumeFrom <= cylinderVolume && p.cylinderVolumeTo >= cylinderVolume
                             select
                             new { price = p.price, ID = p.priceListDistance };
                //).Distinct();
                 foreach (var rec in result)
            {
                System.Diagnostics.Debug.WriteLine("productid: {0}, pricelistduration: {1}, pricelistdistance {2}, isdieselcar {3}, cylindervolume {4}, serviceintervall {5}",
                    productID.ToString(), pricelistduration.ToString(), priceListDistance.ToString(), fueltype.ToString(), cylinderVolume.ToString(), serviceintervall.ToString());
                
                lblBetalningsmetod.Text = string.Format("Månad- {0} {1} + {2} {3} / månad", pricelistduration,rec.price, invoiceFee, countryCurrency);
            }
            }
           

       
        
      //  drpPriceCategory.DataTextField = "Name";
      //  drpPriceCategory.DataValueField = "ID";
      //  drpPriceCategory.DataBind();
    }

    
   


    public class Cylindervolume
    {
        public int Name { get; set; }
    
    }
    private void FillCylinderVolume()
    {
        List<Cylindervolume> lst = new List<Cylindervolume>();
        for (int i = 1000; i < 8100; i=i+100)
        {
            lst.Add(new Cylindervolume { Name = i});
            

          
        }

        drpCylinderVolume.DataSource = lst;
        drpCylinderVolume.DataTextField = "Name";

        drpCylinderVolume.DataBind();
       
    }


    private void FillDealerEmployees(int dealerid)
    {

        flsfragusseEntities1 cEntity = new flsfragusseEntities1();


    }
    private void FillDealers()
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();


    }
    //när vi klickar i datagridden anropas den här funktionen
    public void LoadChangesAndCompare(int vehicleChangeID)
    {

        
         
      
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        tblVehicle_change vehiclechange = null;
        tblVehicle vehicle = null;
        tblPriceList vpricelist = null;
        tblPriceList vpricechange = null;



        //hämta förändringsposten
        List<tblVehicle_change> lstvehiclechange = (from p in cEntity.tblVehicle_change
                                             where p.ID == vehicleChangeID
                                             select p).ToList();
        if (lstvehiclechange.Count() > 0)
        {
          vehiclechange = lstvehiclechange[0];
        


        //först hänmta ut originalposten
        List<tblVehicle> lstvehicle = (from p in cEntity.tblVehicle
                                       where p.vehicleID == vehiclechange.vehicleID
                                       select p).ToList();
        if (lstvehicle.Count() > 0)
        {
           vehicle = lstvehicle[0];
        }


        Fillcombos(vehicle.productID,vehicle.partnerID,vehicle.countryID);


        //hämta ut prislitorna också och jämför för serviceintervall etc.
        //orignalposten
        List<tblPriceList> lstPriceorig = (from p in cEntity.tblPriceList
                                           where p.productID == vehicle.productID &&  p.priceListID==vehicle.priceListID
                                           select p).ToList();

        if (lstPriceorig.Count() > 0)
        {
            vpricelist = lstPriceorig[0];
        }

     
        //hämta ut changeprislista och jämför för serviceintervall etc.
        List<tblPriceList> lstPricechange = (from p in cEntity.tblPriceList
                                             where p.productID == vehiclechange.productID && p.priceListID == vehiclechange.priceListID
                                           select p).ToList();

        if (lstPricechange.Count() > 0)
        {
            vpricechange = lstPricechange[0];
        }





        CompareSets(ref vehiclechange, ref vehicle, ref vpricelist, ref vpricechange);


        FillVehicleInformationToGUI(vehiclechange);

        }

        
    }

    private void FillVehicleInformationToGUI(tblVehicle_change vehiclechange)
    {
              drpCarBrand.SelectedValue=Convert.ToString(vehiclechange.brandID);
              drpModell.SelectedValue = Convert.ToString(vehiclechange.modelID);
              drpFueltype.SelectedValue = Convert.ToString(vehiclechange.isDieselcar);
              drpCylinderVolume.SelectedValue=Convert.ToString(vehiclechange.cylinderVolume);
              drpProduct.SelectedValue = Convert.ToString(vehiclechange.productID);

              txtVinnumber.Text = vehiclechange.vinNumber;
              txtRegnr.Text = vehiclechange.regnr;
              bdpRegdag.SelectedValue = vehiclechange.firstRegistrationDate;

              bdpStartDate.SelectedValue = vehiclechange.leaseStartDate;


                //hämta ut pricelistid
              flsfragusseEntities1 cEntity = new flsfragusseEntities1();
                var pricelist = from p in cEntity.tblPriceList
                          where p.priceListID == vehiclechange.priceListID
                          select p;

                foreach(var rPrice in pricelist)
                {
                    drpServiceIntervall.SelectedValue = Convert.ToString(rPrice.ServiceIntervall);
                    drpDuration.SelectedValue = Convert.ToString(rPrice.priceListDuration);
                    drpDrivingDistance.SelectedValue=Convert.ToString(rPrice.priceListDistance);
                }
            //    vehicle.
     }

    private void ClearChanges()
    {
        spBilmarke.Style["background-color"] = "White";

        spBilmodell.Style["background-color"] = "White";
        spfueltype.Style["background-color"] = "White";


        spRegnr.Style["background-color"] = "White";

        spChassnr.Style["background-color"] = "White";
        spServiceintervall.Style["background-color"] = "White";


        spCylindervolume.Style["background-color"] = "White";

        spRegdag.Style["background-color"] = "White";

        spProduct.Style["background-color"] = "White";

        spDuration.Style["background-color"] = "White";

        spDrivingdistance.Style["background-color"] = "White";
        spStartDate.Style["background-color"] = "White";
    }
   
    

    private void CompareSets(ref tblVehicle_change v, ref tblVehicle c,ref tblPriceList p, ref tblPriceList pc)
    {
        if ( v.brandID != c.brandID)
            spBilmarke.Style["background-color"] = "Red";
        else
            spBilmarke.Style["background-color"] = "White";
     
        if(v.modelID!=c.modelID)
            spBilmodell.Style["background-color"] = "Red";
        else
            spBilmodell.Style["background-color"] = "White";

        if(v.isDieselcar!=c.isDieselcar)
            spfueltype.Style["background-color"] = "Red";
        else
            spfueltype.Style["background-color"] = "White";

        if(v.regnr!=c.regnr)
            spRegnr.Style["background-color"] = "Red";
        else
            spRegnr.Style["background-color"] = "White";

        if (v.vinNumber != c.vinNumber)
            spChassnr.Style["background-color"] = "Red";
        else
            spChassnr.Style["background-color"] = "White";

        if (p.ServiceIntervall != pc.ServiceIntervall)
            spServiceintervall.Style["background-color"] = "Red";
        else
            spServiceintervall.Style["background-color"] = "White";
    
        if(v.cylinderVolume!=c.cylinderVolume)
            spCylindervolume.Style["background-color"] = "Red";
        else
            spCylindervolume.Style["background-color"] = "White";

        if (v.firstRegistrationDate != c.firstRegistrationDate)
            spRegdag.Style["background-color"] = "Red";
        else
            spRegdag.Style["background-color"] = "White";

        if (v.productID != c.productID)
            spProduct.Style["background-color"] = "Red";
        else
            spProduct.Style["background-color"] = "White";

        if(p.priceListDuration!=pc.priceListDuration)
            spDuration.Style["background-color"] = "Red";
        else
            spDuration.Style["background-color"] = "White";

        if(p.priceListDistance!=pc.priceListDistance)
            spDrivingdistance.Style["background-color"] = "Red";
        else
            spDrivingdistance.Style["background-color"] = "White";

        if(v.leaseStartDate!=c.leaseStartDate)
            spStartDate.Style["background-color"] = "Red";
        else
            spStartDate.Style["background-color"] = "White";


       
    }
       
       

     
    

    private void disableTextboxesInBPD(BDPLite BDP)
    {
        ControlCollection controls = BDP.Controls;
        foreach (Control ctrl in controls)
        {
            if (ctrl is TextBox)
            {
                TextBox txtBox = (TextBox)ctrl;

                txtBox.ReadOnly = true;

            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {


        disableTextboxesInBPD(bdpRegdag);
     
    }
    protected void btnapprove_Click(object sender, EventArgs e)
    {
        //då sparar vi posten.

    }
    protected void drpDealers_SelectedIndexChanged(object sender, EventArgs e)
    {
        //if(!filldata)
        //   FillDealerEmployees(Convert.ToInt32(drpDealers.SelectedValue));
    }
    protected void drpFueltype_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(!filldata)
            UpdatePrice();
    }
    protected void drpServiceIntervall_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!filldata)
            UpdatePrice();
    }
    protected void drpCylinderVolume_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!filldata)
            UpdatePrice();
    }
    protected void drpProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!filldata)
        {
            UpdatePrice();
            FillDuration(g_productid);
        }
    }
    protected void drpDuration_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!filldata)
        {
            FillDrivingDistance(g_productid, Convert.ToInt32(drpDuration.SelectedValue));
            UpdatePrice();

        }
    }
    protected void drpDrivingDistance_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!filldata)
            UpdatePrice();
    }
    protected void drpPriceCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!filldata)
            UpdatePrice();
    }
    protected void drpCarBrand_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!filldata)
            FillbrandModel(Convert.ToInt32(drpCarBrand.SelectedValue));
    }
    protected void btnApprove_Click(object sender, EventArgs e)
    {

        //vi sparar över ändringarna och lägger på originalet
        DataKey datakey = grdview.SelectedDataKey;
        if (datakey != null)
        {
            int vehicleChangeID = Convert.ToInt32(datakey.Value);


            flsfragusseEntities1 cEntity = new flsfragusseEntities1();

            //hämta förändringsposten
            List<tblVehicle_change> lstvehiclechange = (from p in cEntity.tblVehicle_change
                                                        where p.ID == vehicleChangeID

                                                        select p).ToList();

            tblVehicle_change vehiclechange = null;
            if (lstvehiclechange.Count() > 0)
            {
                vehiclechange = lstvehiclechange[0];


                //vi måste spara det som ändringsposten pekar på
                SaveVechicle(vehiclechange.ID, vehiclechange.vehicleID, Convert.ToInt32(drpProduct.SelectedValue), Convert.ToInt32(drpDuration.SelectedValue), Convert.ToInt32(drpDrivingDistance.SelectedValue),
                    Convert.ToInt32(drpFueltype.SelectedValue), Convert.ToInt32(drpCylinderVolume.SelectedValue), Convert.ToInt32(drpServiceIntervall.SelectedValue), true,Convert.ToInt32(drpModell.SelectedValue));
            }


        }

    }
    protected void btnDissaprove_Click(object sender, EventArgs e)
    {
        //vi sparar över ändringarna och lägger på originalet
        DataKey datakey = grdview.SelectedDataKey;
        int vehicleChangeID = Convert.ToInt32(datakey.Value);


        flsfragusseEntities1 cEntity = new flsfragusseEntities1();

        //hämta förändringsposten
        List<tblVehicle_change> lstvehiclechange = (from p in cEntity.tblVehicle_change
                                                    where p.ID == vehicleChangeID

                                                    select p).ToList();

        tblVehicle_change vehiclechange = null;
        if (lstvehiclechange.Count() > 0)
        {
            vehiclechange = lstvehiclechange[0];

        }
        //vi måste spara det som ändringsposten pekar på
        SaveVechicle(vehiclechange.ID, vehiclechange.vehicleID, Convert.ToInt32(drpProduct.SelectedValue), Convert.ToInt32(drpDuration.SelectedValue), Convert.ToInt32(drpDrivingDistance.SelectedValue),
            Convert.ToInt32(drpFueltype.SelectedValue), Convert.ToInt32(drpCylinderVolume.SelectedValue), Convert.ToInt32(drpServiceIntervall.SelectedValue), false, Convert.ToInt32(drpModell.SelectedValue));

    }
   
    protected void btnTerminate_Click(object sender, EventArgs e)
    {
       // TerminateContract(4662);
    }

    //fylla i värdena 
    private void FillVehicle(int vehicleID)
    {

        flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        var vehicle = from p in cEntity.tblVehicle
                      where p.vehicleID == vehicleID
                      select p;

        //   new { Name = p.brand, ID = p.brandID };

        //fillvalues
        foreach (var vec in vehicle)
        {

            vehicleID = vec.vehicleID;
            drpCarBrand.SelectedValue = Convert.ToString(vec.brandID);
            drpModell.SelectedValue = Convert.ToString(vec.modelID);
            drpFueltype.SelectedValue = Convert.ToString(vec.isDieselcar);
            drpCylinderVolume.SelectedValue = Convert.ToString(vec.cylinderVolume);

            drpProduct.SelectedValue = Convert.ToString(vec.productID);


            txtVinnumber.Text = vec.vinNumber;
            txtRegnr.Text = vec.regnr;
            bdpRegdag.SelectedValue = vec.firstRegistrationDate;

            bdpStartDate.SelectedValue = vec.leaseStartDate;


            //hämta ut pricelistid
            //  flsfragusseEntities1 cEntity = new flsfragusseEntities1();
            var pricelist = from p in cEntity.tblPriceList
                            where p.priceListID == vec.priceListID
                            select p;

            foreach (var rPrice in pricelist)
            {
                drpServiceIntervall.SelectedValue = Convert.ToString(rPrice.ServiceIntervall);
                drpDuration.SelectedValue = Convert.ToString(rPrice.priceListDuration);
                drpDrivingDistance.SelectedValue = Convert.ToString(rPrice.priceListDistance);
            }
            //    vehicle.
        }


    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        Response.Redirect("partnerConfirmPartnerChanges.aspx");
    }
}